import * as echarts from '../../ec-canvas/echarts';
import { financeCount, settlement } from '../../api/index'
import { getYMDHMS } from '../../utils/util'
function setOption(chart, piedata) {
  var option = {
    title: {
      text: piedata.dayMoney,
      left: '30%',
      top: 'center',
      textStyle: {
        color: '#ff00ff',
        fontSize: 16,
        width: 140,
        overflow: 'truncate',
        ellipsis: '...'
      },
      textAlign: 'center'
    },
    tooltip: {
      trigger: 'item'
    },
    legend: {
      top: 'center',
      right: '0',
      orient: 'vertical'
    },
    series: [
      {
        type: 'pie',
        radius: ['50%', '60%'],
        center: ['30%', '50%'],
        avoidLabelOverlap: false,
        label: { show: false },
        labelLine: { show: false },
        data: piedata
      }
    ]
  };

  chart.setOption(option);
  return chart;
}

Page({
  data: {
    ec: {
      //onInit: initChart
      // 将 lazyLoad 设为 true 后，需要手动初始化图表
      lazyLoad: true
    },
    dayMoney: '',
    kssj: getYMDHMS(new Date()).slice(0, 10),
    nowe: getYMDHMS(new Date()).slice(0, 10),
    thead: [{
      title: '收入方式',
      key: 'index',
      width: '300'
    }, {
      title: '会员卡',
      key: 'index',
      width: '150'
    }, {
      title: '支付宝',
      key: 'index',
      width: '150'
    }, {
      title: '微信',
      key: 'index',
      width: '150'
    }, {
      title: '现金',
      key: 'index',
      width: '150'
    }, {
      title: '转账',
      key: 'index',
      width: '150'
    }, {
      title: '总计',
      key: 'index',
      width: '150'
    }],
    dataSources: [],
    show: false,
    des: ''
  },
  getUserInfo(event) {
    console.log(event.detail);
  },
  submit() {
    this.setData({ show: true });
  },
  onClose() {
    this.setData({ show: false });
  },
  initpie: function (piedata, type) {
    if (type == 1) {
      let ecComponent = this.selectComponent('#mychart-dom-pie');
      ecComponent.init((canvas, width, height, dpr) => {
        // 获取组件的 canvas、width、height 后的回调函数
        // 在这里初始化图表
        const chart = echarts.init(canvas, null, {
          width: width,
          height: height,
          devicePixelRatio: dpr // new
        });
        //调用设定EChart报表状态的函数，并且把从后端拿到的数据传过去
        setOption(chart, piedata);
        // 注意这里一定要返回 chart 实例，否则会影响事件处理等
        return chart;
      });
    } else {
      let ecComponentb = this.selectComponent('#mychart-dom-pieb');
      ecComponentb.init((canvas, width, height, dpr) => {
        // 获取组件的 canvas、width、height 后的回调函数
        // 在这里初始化图表
        const chart = echarts.init(canvas, null, {
          width: width,
          height: height,
          devicePixelRatio: dpr // new
        });
        //调用设定EChart报表状态的函数，并且把从后端拿到的数据传过去
        setOption(chart, piedata);
        // 注意这里一定要返回 chart 实例，否则会影响事件处理等
        return chart;
      });
    }


  },
  bindTimeChange: function (e) {
    this.setData({
      kssj: e.detail.value
    })
    this.financeCount()
  },
  bindTextAreaBlur: function (e) {
    this.setData({
      des: e.detail.value
    })
  },
  onConfirm() {
    settlement({
      param: { venueId: wx.getStorageSync('GYMID'),
      startTime: this.data.kssj + ' 00:00:00',
        endTime: this.data.kssj + ' 23:59:59',
      settlementDate: this.data.kssj + ' 23:59:59', settlementInfo: this.data.des }
    }).then(res => {
      if (res.errorCode != '000000') return
      wx.showToast({ title: '结算成功', icon: 'none', duration: 2000 })
      setTimeout(() => {
        wx.navigateBack({
          delta: 1,
        })
      }, 2000);
    })
  },
  financeCount() {
    financeCount({
      param: {
        userId: wx.getStorageSync('userId'),
        venueId: wx.getStorageSync('GYMID'),
        startTime: this.data.kssj + ' 00:00:00',
        endTime: this.data.kssj + ' 23:59:59',
        settlementDate: this.data.kssj + ' 23:59:59'
      }
    }).then(res => {
      if (res.errorCode != '000000') return
      let incomeMethod = []
      Object.keys(res.record.incomeMethod).forEach((key) => {
        const value = parseFloat(res.record.incomeMethod[key])
        incomeMethod.push({ value: value, name: key + '：' + value })
      })
      let paymentMethod = []
      Object.keys(res.record.paymentMethod).forEach((key) => {
        const value = parseFloat(res.record.paymentMethod[key])
        paymentMethod.push({ value: value, name: key + '：' + value })
      })
      let settleList = []
      let lastItem = []
      let dayMoney = ''
      for (const item of res.record.list) {
        if (item.status === '66') {
          lastItem = item
          dayMoney = item.allMoney
        } else {
          settleList.push(item)
        }
      }
      settleList.push(lastItem)
      incomeMethod.dayMoney = dayMoney
      paymentMethod.dayMoney = dayMoney
      this.setData({ dayMoney,dataSources:res.record.list })
      this.initpie(incomeMethod, 1);
      this.initpie(paymentMethod, 2);
    })

  },
  onLoad: function (options) {
    this.financeCount()
    //   var piedata = [
    //     { value: Number(342), name: '固定场地：' + Number(44) + '小时' },
    //     { value: Number(44), name: '前台预定：' + Number(22) + '小时' },
    //     { value: Number(44), name: '网络预定：' + Number(23) + '小时' }
    //   ];
    //  this.initpie(piedata);
  }
});

